#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

/* 
	length of string is at most 35
	d[i][j] = min/max evaluation possible from s[i] to s[j]
	for easy manipulation, we add 2 plus signs before and after string s
*/

#define oo 10000000000000000LL
long long d[36][36];
string s;
int ntest;

bool isSign(char c)
{
	return ((c == '+') || (c == '*'));
}

int main()
{
	scanf("%d\n", &ntest);
	while (0 < ntest--)
	{
		getline(cin, s);
		s = '+' + s + '+';
	
		for (int i = 1; i < (signed) s.size()-1; i++)
			if (isSign(s[i+1]))
			{
				if (isSign(s[i-1])) d[i][i] = s[i] - '0';
				else
					if (isSign(s[i-2]))
						d[i-1][i] = (s[i-1] - '0')*10 + s[i] - '0';
			}

		for (int len = 3; len <= (signed) s.size()-2; len++)
			for (int start = 1; start+len-1 <= (signed) s.size()-2; start++)
			{
				if (!isSign(s[start-1])) continue;
				
				int end = start + len - 1;
				
				if (!isSign(s[end+1])) continue;
				
				d[start][end] = oo;
								
				for (int signPos = start + 1; (signed) signPos < end; signPos++)
					if (isSign(s[signPos]))
					{
						if (s[signPos] == '+')
							d[start][end] = min(d[start][end], d[start][signPos-1] + d[signPos+1][end]);
						else
							d[start][end] = min(d[start][end], d[start][signPos-1] * d[signPos+1][end]);
					}
				//cout << start << " " << end << " " << d[start][end] << endl;
			}
		long long minValue = d[1][s.size()-2];

		for (int len = 3; len <= (signed) s.size()-2; len++)
			for (int start = 1; start+len-1 <= (signed) s.size()-2; start++)
			{
				if (!isSign(s[start-1])) continue;
				
				int end = start + len - 1;
				
				if (!isSign(s[end+1])) continue;
				
				d[start][end] = -oo;
								
				for (int signPos = start + 1; (signed) signPos < end; signPos++)
					if (isSign(s[signPos]))
					{
						if (s[signPos] == '+')
							d[start][end] = max(d[start][end], d[start][signPos-1] + d[signPos+1][end]);
						else
							d[start][end] = max(d[start][end], d[start][signPos-1] * d[signPos+1][end]);
					}
			}
		long long maxValue = d[1][s.size()-2];

		cout << "The maximum and minimum are " << maxValue << " and " << minValue << "." << endl;
	}
	return 0;
}
